package team.yidong.fenda.wx.controller;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import team.yidong.fenda.entity.Hospital;
import team.yidong.fenda.entity.Master;
import team.yidong.fenda.entity.User;
import team.yidong.fenda.repository.MasterRepository;
import team.yidong.fenda.service.MasterService;
import team.yidong.fenda.service.UserService;
import team.yidong.fenda.utils.WxLoginUtil;
import team.yidong.fenda.wx.Service.WxHospitalService;
import team.yidong.fenda.wx.Service.WxUserService;

import java.util.UUID;

/**
 * Created by xuanlong on 2017/11/11.
 */
@Controller
public class WxLoginController {
    @Autowired
    WxLoginUtil wxLoginUtil;
    @Autowired
    MasterService masterService;
    @Autowired
    WxUserService wxuserService;
    @Autowired
    WxHospitalService wxHospitalService;

    @RequestMapping("/wx/checkLoginTime")
    @ResponseBody
    public String checkTime(String login_uuId) {

        Jedis jedis = new Jedis("localhost");
        if (jedis.get(login_uuId) == null) {
            return "error";
        } else {
            jedis.expire(login_uuId, 60 * 60 * 24 * 30);
            return "success";
        }
    }

    /**
     * 问必得收费版
     *
     * @param code
     * @return
     */
    @RequestMapping("/wx/wxLogin")
    @ResponseBody
    public String wxLogin(@RequestParam String code) {
        System.out.println("我在缓存中设置了openId");
        String string = wxLoginUtil.Get("https://api.weixin.qq.com/sns/jscode2session?appid=wx697ed96969c6707f&secret=9f1cc13b4ecbc55ae5c9bd0285ddd8b8&js_code=" + code + "&grant_type=authorization_code");

        Jedis jedis = new Jedis("localhost");
        JSONObject jsonObject = JSONObject.fromObject(string);
        String openid = jsonObject.getString("openid");
        if (wxuserService.findByOpenId(openid) == null) {
            User user = new User();
            user.setHeadImg("https://www.liuxuanlong.club/u=283967351,1011371375&fm=27&gp=0.jpg");
            user.setName("小知识分子");
            user.setIsBlack("否");
            Hospital hospital = wxHospitalService.findWxHospital(2);
            user.setUserOpenId(openid);
            user.setHospital(hospital);
            wxuserService.addUser(user);
        }
        String session_key = jsonObject.getString("session_key");
        String uid = UUID.randomUUID().toString();
        StringBuffer sb = new StringBuffer();
        sb.append(openid);
        sb.append("," + session_key);
        jedis.set(uid, openid);
        jedis.expire(uid, 60 * 60 * 24 * 30);
        return uid;

    }

    /**
     * 专家端
     *
     * @param code
     * @return
     */
    @RequestMapping("/wx/wxLoginMaster")
    @ResponseBody
    public String wxLoginMaster(@RequestParam String code) {
        System.out.println("我在缓存中设置了openId");
        String string = wxLoginUtil.Get("https://api.weixin.qq.com/sns/jscode2session?appid=wxe0f9c7f32132f75a&secret=5d93b925630edc021682824c44e2692a&js_code=" + code + "&grant_type=authorization_code");

        Jedis jedis = new Jedis("localhost");
        JSONObject jsonObject = JSONObject.fromObject(string);
        String openid = jsonObject.getString("openid");
        String session_key = jsonObject.getString("session_key");
        String uid = UUID.randomUUID().toString();
        StringBuffer sb = new StringBuffer();
        sb.append(openid);
        sb.append("," + session_key);
        jedis.set(uid, openid);
        jedis.expire(uid, 60 * 60 * 24 * 30);
        return uid;

    }

    /**
     * 问必得免费版
     *
     * @param code
     * @return
     */
    @RequestMapping("/wx/wxLoginFree")
    @ResponseBody
    public String wxLoginFree(@RequestParam String code) {
        System.out.println("我在缓存中设置了openId");
        String string = wxLoginUtil.Get("https://api.weixin.qq.com/sns/jscode2session?appid=wx198ae9b796da2183&secret=68e814aebaf9c7f7947216036f17806f&js_code=" + code + "&grant_type=authorization_code");

        Jedis jedis = new Jedis("localhost");
        JSONObject jsonObject = JSONObject.fromObject(string);
        String openid = jsonObject.getString("openid");
        if (wxuserService.findByOpenId(openid) == null) {
            User user = new User();
            user.setHeadImg("https://www.liuxuanlong.club/u=283967351,1011371375&fm=27&gp=0.jpg");
            user.setName("小知识分子");
            user.setIsBlack("否");
            Hospital hospital = wxHospitalService.findWxHospital(2);
            user.setUserOpenId(openid);
            user.setHospital(hospital);
            wxuserService.addUser(user);
        }
        String session_key = jsonObject.getString("session_key");
        String uid = UUID.randomUUID().toString();
        StringBuffer sb = new StringBuffer();
        sb.append(openid);
        sb.append("," + session_key);
        jedis.set(uid, openid);
        jedis.expire(uid, 60 * 60 * 24 * 30);
        return uid;

    }

    /**
     * 法律版
     *
     * @param code
     * @return
     */
    @RequestMapping("/wx/wxLoginFalv")
    @ResponseBody
    public String wxLoginFalv(@RequestParam String code) {
        System.out.println("我在缓存中设置了openId");
        String string = wxLoginUtil.Get("https://api.weixin.qq.com/sns/jscode2session?appid=wxeebd2de131ff2f5f&secret=58ea4ec32afb62696d8782554e78ee33&js_code=" + code + "&grant_type=authorization_code");

        Jedis jedis = new Jedis("localhost");
        JSONObject jsonObject = JSONObject.fromObject(string);
        String openid = jsonObject.getString("openid");
        if (wxuserService.findByOpenId(openid) == null) {
            User user = new User();
            user.setHeadImg("https://www.liuxuanlong.club/u=283967351,1011371375&fm=27&gp=0.jpg");
            user.setName("小知识分子");
            user.setIsBlack("否");
            Hospital hospital = wxHospitalService.findWxHospital(6);
            user.setUserOpenId(openid);
            user.setHospital(hospital);
            wxuserService.addUser(user);
        }
        String session_key = jsonObject.getString("session_key");
        String uid = UUID.randomUUID().toString();
        StringBuffer sb = new StringBuffer();
        sb.append(openid);
        sb.append("," + session_key);
        jedis.set(uid, openid);
        jedis.expire(uid, 60 * 60 * 24 * 30);
        return uid;

    }

    @RequestMapping("/wx/checkMaster")
    @ResponseBody
    public String checkMaster(@RequestParam String wxNum) {

        Jedis jedis = new Jedis("localhost");
        wxNum = jedis.get(wxNum);
        Master master = masterService.findByOpenId(wxNum);
        if (master == null) {

            return "no";
        } else {
            return "yes";
        }
    }
}
